<template>
  <a-modal
    title="排序"
    :width="480"
    :maskClosable="false"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" v-bind="formItemLayout">
        <a-form-item label="序号">
          <a-input-number
            style="width: 100%;"
            v-decorator="[
              'serialNum',
              {
                rules: [{ required: true, message: '请输入序号！' }]
              }
            ]"
            :min="1"
            :max="99999999"
            :precision="0"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { sortUser } from '@/api/authenticationCenter/structure';

export default {
  name: 'UserSort',
  data() {
    return {
      formItemLayout: {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 18 }
        }
      },
      record: null,
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this)
    };
  },
  props: {
    orgId: {
      type: Number,
      required: true
    }
  },
  methods: {
    open(record, serialNum) {
      this.visible = true;
      this.record = record;
      this.$nextTick(() => {
        this.form.setFieldsValue({
          serialNum: serialNum || null
        });
      });
    },
    handleSubmit() {
      const {
        form: { validateFields, getFieldsValue }
      } = this;
      this.confirmLoading = true;
      validateFields(async (errors, values) => {
        if (!errors) {
          const req = { ...values };
          req.id = this.record;
          req.orgId = this.orgId;

          const res = await sortUser(req);
          this.confirmLoading = false;
          if (res.code === 'sso.ok') {
            this.$emit('ok');
            this.handleCancel();
            this.$message.success(res.message);
          }
        } else {
          this.confirmLoading = false;
        }
      });
    },
    handleCancel() {
      if (this.confirmLoading) return;
      this.visible = false;
      this.record = null;
      const {
        form: { resetFields }
      } = this;
      resetFields();
    }
  }
};
</script>
